package com.gutai.admin.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gutai.admin.common.entity.dto.SelectListDto;
import com.gutai.admin.common.entity.response.Response;
import com.gutai.admin.common.util.ExceptionUtils;
import com.gutai.admin.common.util.ResponseHelper;
import com.gutai.admin.entity.AttributeCategory;
import com.gutai.admin.service.IAttributeCategoryService;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@RestController
@RequestMapping("/attributeCategory")
public class AttributeCategoryController {

    private static final Logger logger = LoggerFactory.getLogger(AttributeCategoryController.class);

    @Autowired
    private IAttributeCategoryService attributeCategoryService;
    @CrossOrigin
    @GetMapping("/listAll")
    public Response listAll(String name) {
        try {
            List<AttributeCategory> list = null;
            if (StringUtils.isNotBlank(name)) {
                list = attributeCategoryService.getList(name);
            } else {
                list = attributeCategoryService.getAll();
            }
            return ResponseHelper.buildOk(list);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/selectList")
    public Response selectList() {
        try {
            List<AttributeCategory> list = attributeCategoryService.getAll();
            List<SelectListDto> selectList = list.stream()
                    .sorted(Comparator.comparingLong(AttributeCategory::getCreateTime).reversed())
                    .map(data -> new SelectListDto(data.getId(), data.getName()))
                    .collect(Collectors.toList());
            return ResponseHelper.buildOk(selectList);
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }
    
    @CrossOrigin
    @PostMapping("/add")
    public Response add(@RequestBody AttributeCategory entity) {
        try {
            attributeCategoryService.add(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败:" + e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/update")
    public Response update(@RequestBody AttributeCategory entity) {
        try {
            attributeCategoryService.update(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败:" + e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/delete")
    public Response delete(Integer id) {
        try {
            attributeCategoryService.delete(id);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败:" + e.getMessage());
        }
    }
}
